 //
//  QAIMViewController.m
//  Pods
//
//  Created by 范浩云 on 2018/3/13.
//
//


#import "QAIMViewController.h"
#import "QAIMNavigaionView.h"
#import "QAIMessageTableViewCell.h"
#import <ImSDK/ImSDK.h>
#import <TLSSDK/TLSHelper.h>
#import "QAIMGameMangaer.h"
#import "QAIMChatViewController.h"
#import "QAIMConversationDeleTableView.h"
#import "QAIMHttpRequest.h"

#define ContentViewTop  180
#define BannerWidth    275
#define BannerHeight    150
#define HeaderViewBottom     15
#define QStatusBarHeight    (QG_IS_IPHONE_X ? 44.0 : 20.0)
#define QNavBarHeight  (44.0)
#define QCustomNavBarHeight  (QStatusBarHeight + QNavBarHeight)
#define QG_IS_IPHONE_X (QDEVICE_WIDTH == 375.f && QDEVICE_HEIGHT == 812.f)
#define QPHOTO_FRAME_WIDTH   10
#define QDEVICE_HEIGHT      [UIScreen mainScreen].bounds.size.height
#define QDEVICE_WIDTH       [UIScreen mainScreen].bounds.size.width
#define QFONT(Size) [UIFont fontWithName:FONT_NAME size:Size]

@interface QAIMViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property (nonatomic, strong)  QAIMNavigaionView * naView;
@property(nonatomic,strong)UITableView * messageTableView;
@property (nonatomic, strong)QAIMGameMangaer * gameImManager;

@end


@implementation QAIMViewController


#pragma mark - ********************** View Lifecycle **********************

- (void)dealloc
{
    // 一定要关注这个函数是否被执行了！！！
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    
    
    // 初始化变量
    [self initVariable];
    
    // 初始化界面
    [self initViews];
    
    // 注册消息
    [self regNotification];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


#pragma mark - ********************** init and config **********************

/**
 TODO: 初始化变量，例如：分页的page可以在该函数中赋初值
 */
- (void)initVariable
{
    __weak typeof(self) weakSelf =self;
    /// 加入聊天室
    _gameImManager = [QAIMGameMangaer shareGameManger];
    
}

- (void)initViews
{
    // 初始化Nav导航栏
    [self initNavView];
    // 创建相关子view
    [self initMainViews];
}

/**
 TODO: 初始化Nav导航栏
 */
- (void)initNavView
{
    self.naView = [[QAIMNavigaionView alloc] initWithFrame:CGRectMake(0, 0, QDEVICE_WIDTH, QCustomNavBarHeight)];
    _naView.titleLabel.text = @"游戏消息";
    self.naView.rightButton.hidden = YES;
    __weak typeof(self) weakSelf = self;
    _naView.blockBtnClick = ^(UIButton *btn, QAE_ClickType type) {
        switch (type) {
                case QAE_BackBtnClick:
            {
                NSLog(@"返回");
                [weakSelf backButtonClicked:btn];
            }
                break;
                case QAE_RightBtnClick:
            {
                
            }
                break;
            default:
                break;
        }
    };
    [self.view addSubview:_naView];
}

/**
 TODO: 创建相关子view
 */
- (void)initMainViews
{
    [self.view addSubview:self.messageTableView];
}

/**
 TODO: 注册通知
 */
- (void)regNotification
{
    
    
}


#pragma mark - ******************************************* 对外方法 **********************************


#pragma mark - ******************************************* 基类方法(一般发生在重写函数) ****************


#pragma mark - ******************************************* Touch Event ***********************

/**
 TODO: 返回按钮点击事件的响应函数
 */
- (void)btnClickBack:(UIButton *)aSender
{
    [self.navigationController popViewControllerAnimated:YES];
}



/**
 TODO: 返回按钮点击事件的响应函数
 
 @param sender 被点击的按钮
 */
- (void)backButtonClicked:(id)sender
{
    if (self.navigationController.presentingViewController != nil)
    {
        [self.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - ******************************************* 私有方法 **********************************


#pragma mark - ******************************************* Net Connection Event ********************

#pragma mark - 请求 demo

- (void)req_url_demo
{
    
}


#pragma mark - ******************************************* Delegate Event **************************

#pragma mark - 代理 demo

- (void)delegate_demo
{
    
}
#pragma mark - tableViewDelegate -----------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QAIMessageTableViewCell * messageCell = [tableView dequeueReusableCellWithIdentifier:@"messageCell" forIndexPath:indexPath];
    if (!messageCell) {
        messageCell = [[QAIMessageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"feedTableViewCell"];
    }
    [messageCell setCellContent];
    return messageCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QAIMChatViewController * chatRoom = [[QAIMChatViewController alloc] init];
    [self.navigationController pushViewController:chatRoom animated:YES];
}




#pragma mark - ******************************************* Notification Event **********************

#pragma mark - 通知 demo

- (void)notification_demo:(NSNotification *)aNotification
{
    
}


#pragma mark - ******************************************* 属性变量的 Set 和 Get 方法 *****************

- (UITableView*)messageTableView
{
    if (!_messageTableView)
    {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,64, QDEVICE_WIDTH, QDEVICE_HEIGHT-64) style:UITableViewStylePlain];
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.userInteractionEnabled = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView registerClass:[QAIMessageTableViewCell class] forCellReuseIdentifier:@"messageCell"];
        _messageTableView = tableView;
    }
    return _messageTableView;
}


-(UIStatusBarStyle)preferredStatusBarStyle

{
    return UIStatusBarStyleLightContent;
}

@end
